﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LeMigue.TeamPortal.Base.Biz.Interface;
using LeMigue.TeamPortal.Base.View;
using LeMigue.Scrum.Iterface.Model;
using LeMigue.Scrum.Iterface.Enum;
using LeMigue.Scrum.Iterface.Service;
using LeMigue.TeamPortal.Base.Service;


namespace LeMigue.TeamPortal.Base.Biz.Implement
{
    public class TaskBiz : ITaskBiz
    {
        
        ITaskService _taskService = FactoryService.TaskService;

        IMergeBiz _mergBiz = new MergeBiz();//TODO: implementar o unity

        public TaskView Load(int? id)
        {
            //TODO: tratamento das exceções
            if(!id.HasValue)
                throw new Exception("encontrar a exceção mais adequada");

            return new TaskView(_taskService.Load(id.Value));
        }


        public IList<TaskView> List(IList<Task> tasks, TaskState? state)
        {
            if(state.HasValue)
                //Se tiver algum filho com este status eu trago a tarefa independente do status
                return tasks.Where(i => i.State == state.Value || HasSubState(i, state.Value)).Select(i => new TaskView(i)).ToList();
            else
                return tasks.Select(i => new TaskView(i)).ToList();
        }

        public bool HasSubState(Task task, TaskState state)
        {
            if (task.HasTask)
            {
                return task.Tasks.Where(t => t.State == state || HasSubState(t, state)).Count() > 0;
            }
            return false;
        }

        public IList<TaskView> List(Sprint sprint, TaskState? state)
        {
            return List(sprint.Tasks, state);
        }

        public IList<TaskView> List(Bug bug, TaskState? state)
        {
            return List(bug.Tasks, state);
        }

        public IList<TaskView> List(ItemBacklog backlog, TaskState? state)
        {
            return List(backlog.Tasks, state);
        }


        public IList<TaskView> List(IHasTask item, TaskState? state)
        {
            return List(item.Tasks, state);
        }


        public Task Save(Task taskOrigem)
        {
            Task newTask;


            Task destinoTask = _taskService.Load(taskOrigem.Id);

            string[] propertyToSave = new string[] { "Title", "Description", "AssingTo", "RemainingWork" };

            newTask = _mergBiz.Merge<Task>(taskOrigem, destinoTask, propertyToSave);

            //TODO: esta validação tá meio estranha
            if (newTask == null)
                return taskOrigem;

            _taskService.Save(newTask);

            return newTask;

        }
        public Task Create(Task newTask, Iteration iteracao, Team team)
        {
            newTask.Iteration = iteracao;
            newTask.Team = team;

            _taskService.Save(newTask);

            return newTask;
        }
        public TaskView New(int? idParent)
        {
            Task newTask = new Task();
            newTask.Parent = idParent.HasValue ? idParent.Value : 0;
            return new TaskView(newTask);
        }
    }
}
